package com.os.opencv.java.chapter5;

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class PerspectiveTransform {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //读取图像文件并在屏幕上显示
        Mat src = Imgcodecs.imread("/Users/matt/MATT-MAC/matt_workspace/OS-OpenCV_workspace/scl.jpg");

        //定义原图相中4个点的坐标
        Point[] pt1 = new Point[4];
        pt1[0] = new Point(132.56637168141594, 480.2654867256637);
        pt1[1] = new Point(698.9380530973451, 201.50442477876106);
        pt1[2] = new Point(1893.6283185840707, 533.362831858407);
        pt1[3] = new Point(1302.9203539823009, 891.7699115044247);

        //定义目标图像中4个点的坐标
        Point[] pt2 = new Point[4];
        pt2[0] = new Point(0, 0);
        pt2[1] = new Point(698.9380530973451, 0);
        pt2[2] = new Point(1893.6283185840707, 533.362831858407);
        pt2[3] = new Point(1302.9203539823009, 891.7699115044247);

        //getPerspectiveTransform()函数用到的数据类型
        MatOfPoint2f mop1 = new MatOfPoint2f(pt1);
        MatOfPoint2f mop2 = new MatOfPoint2f(pt2);

        //计算透视变换矩阵并进行仿射变换
        Mat dst = new Mat();
        Mat matrix = Imgproc.getPerspectiveTransform(mop1, mop2);
        //获取转换矩阵
        Imgproc.warpPerspective(src, dst, matrix, new Size(500, 900));

        //在屏幕上显示变换前后的图形
        HighGui.imshow("src", src);
        HighGui.waitKey(0);
        HighGui.imshow("dst", dst);
        HighGui.waitKey(0);

        System.exit(0);
    }
}
